//
//  LanguageSelectTableViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/9.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "LanguageSelectTableViewController.h"
#import "LanguageSelectTableCell.h"

@interface LanguageSelectTableViewController ()
/// <#注释#>
@property (nonatomic, strong) NSArray *languageList;
/// <#注释#>
@property (nonatomic, copy) NSString *curLanguageKey;
@end

@implementation LanguageSelectTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configLanguAgeInfo];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)setupUI{
    [super setupUI];
    self.title = LCLocalString(@"language");
    self.curLanguageKey = [LanguageManager userLanguage];
    [self.tableView registerNib:[UINib nibWithNibName:@"LanguageSelectTableCell" bundle:nil] forCellReuseIdentifier:@"LanguageSelectTableCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
- (void)configLanguAgeInfo{
    NSArray *languageList = @[
//        @{@"name":@"跟随系统",@"key":@"system",},
        @{@"name":@"English",@"key":@"en",},
        @{@"name":@"繁体中文",@"key":@"zh-Hant",},
//        @{@"name":@"简体中文",@"key":@"zh-Hans",}
    ];
    
    self.languageList = languageList;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.languageList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *languageM = self.languageList[indexPath.row];
    LanguageSelectTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LanguageSelectTableCell"];
    cell.title.text = languageM[@"name"];
    NSString *imgName = [self.curLanguageKey isEqualToString:languageM[@"key"]] ? @"yuan_xz":@"yuan_wxz";
    cell.icon.image = [UIImage imageNamed:imgName];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *key = self.languageList[indexPath.row][@"key"];
    
    if ([self.curLanguageKey isEqualToString:key]) {
        return;
    }
    [LanguageManager setUserlanguage:key];
    self.curLanguageKey = key;
    [self.tableView reloadData];
}


@end
